# computer_client.py
import requests
import time
import os
from modules.printer import print_content

SERVER_URL = "http://???:???"
COMPUTER_ID = "test-computer-id"  # 每台电脑应有唯一ID


def register_computer():
    """向服务器注册电脑"""
    payload = {'computer_id': COMPUTER_ID}
    try:
        response = requests.post(f"{SERVER_URL}/register_computer", json=payload)
        if response.json().get('status') == 'success':
            print("Computer registered successfully")
        else:
            print("Registration failed")
    except Exception as e:
        print(f"Registration error: {e}")


def check_print_tasks():
    """检查并执行打印任务"""
    while True:
        try:
            response = requests.get(
                f"{SERVER_URL}/get_print_task",
                params={'computer_id': COMPUTER_ID}
            )
            data = response.json()

            if data['status'] == 'success':
                task = data['data']
                print(f"Printing: {task['content']['skuOuterId']}")
                print_content(task['content']['skuOuterId'])
            else:
                print("暂无打印任务")

        except Exception as e:
            print(f"Error checking tasks: {e}")

        time.sleep(5)  # 每5秒检查一次新任务


if __name__ == '__main__':
    register_computer()
    check_print_tasks()